package com.uenpay.im.ui.old.activity;

import android.content.Intent;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.aswife.ui.MaskImageView;
import com.qiyunxin.android.http.common.ImageUtil;
import com.qiyunxin.android.http.utils.FileUtils;
import com.qiyunxin.android.http.utils.Utils;
import com.socks.library.KLog;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.entities.ImageSize;
import com.uenpay.util.app.base.BaseActivity;
import com.uenpay.util.imageloader.ImageLoader;
import com.uenpay.util.imageloader.ImageLoaderUtil;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class CameraActivity extends BaseActivity {

    private final int CHOOSE_TAKE_PHOTO_REQUEST_CODE = 110;
    private String currentPhotoPath;
    private FileUtils fileUtils = new FileUtils(IMApplication.appName);
    private TextView rightText, middleText;

    private View top_view;

    private ImageView photo_img;

    private String big_pic_filename;

    private ArrayList<ImageSize> sizes;
    private Camera camera = null;
    private View how_get_permission_layout;

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_select_local_photo;
    }

    @Override
    public void initView(View view) {
        Bundle bundle = getIntent().getExtras();
        sizes = (ArrayList<ImageSize>) bundle.getSerializable("size");
        initView();
        initListener();

        if (getCameraInstance() != null) {
            camera.release();
            Intent getImageByCamera = new Intent(
                    MediaStore.ACTION_IMAGE_CAPTURE);
            try {
                File f = fileUtils.createImageFile();
                currentPhotoPath = f.getAbsolutePath();
                getImageByCamera.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(f));
                startActivityForResult(getImageByCamera,
                        CHOOSE_TAKE_PHOTO_REQUEST_CODE);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private Camera getCameraInstance() {
        try {
            camera = Camera.open();
        } catch (Exception e) {
            rightText.setVisibility(View.GONE);
            middleText.setText(R.string.not_read_camera);
            top_view.setVisibility(View.VISIBLE);
            photo_img.setVisibility(View.GONE);
            how_get_permission_layout.setVisibility(View.VISIBLE);
        }
        return camera;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            if (requestCode == CHOOSE_TAKE_PHOTO_REQUEST_CODE) {
                try {
                    big_pic_filename = fileUtils.getAlbumDir() + "/"
                            + Utils.md5(currentPhotoPath) + ".jpg";

//                    if (sizes != null) {
//
//                        for (int i = 0; i < sizes.size(); i++) {
//                            ImageSize size = sizes.get(i);
//                            ImageUtil.CompressImageAndSave(currentPhotoPath,
//                                    big_pic_filename + size.name, 80,
//                                    size.width, size.height);
//                        }
//                    }
//                    photo_img.SetUrl(big_pic_filename);
                    KLog.d(TAG, "currentPhotoPath-->" + currentPhotoPath);
//                    photo_img.SetUrl(currentPhotoPath);
//                    ImageLoaderUtil.getInstance().loadImage(this, new ImageLoader.Builder().imgView(photo_img).url(currentPhotoPath).build());
//                    photo_img.setVisibility(View.VISIBLE);
//                    top_view.setVisibility(View.VISIBLE);
//                    findViewById(R.id.title_right_layout).setVisibility(
//                            View.VISIBLE);
                    Intent PhotoData = new Intent();
                    PhotoData.putExtra("big_pic_filename", currentPhotoPath);
                    setResult(RESULT_OK, PhotoData);
                    finish();
                } catch (Exception e) {

                    e.printStackTrace();
                }
            }
        } else {
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    protected void initView() {
        how_get_permission_layout = findViewById(R.id.how_get_permission_layout);
        top_view = findViewById(R.id.top_view);
        top_view.setVisibility(View.GONE);
        photo_img = (ImageView) findViewById(R.id.photo_img);
        rightText = (TextView) findViewById(R.id.title_right_tv);
        rightText.setText(R.string.sure);
        middleText = (TextView) findViewById(R.id.title_textview);
        middleText.setText(R.string.yulan);
    }

    protected void initListener() {
        findViewById(R.id.title_right_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent data = new Intent();
                        data.putExtra("big_pic_filename", currentPhotoPath);
                        setResult(RESULT_OK, data);

                        finish();

                    }
                });
    }
}
